from django.shortcuts import render,HttpResponse,redirect
from .models import users
from .tests import send_email

# Create your views here.


def index(request):
    request.session.set_expiry(0)  # 关闭浏览器后失效
    return render(request, 'index.html')
def login_user(request):#登录
    if request.method == "POST":
        print("login....")
        email = request.POST.get('email')
        password = request.POST.get('password')
        message = "所有字段都必须填写！"
        if email and password:  # 确保用户名和密码都不为空
            email = email.strip()
            # 用户名字符合法性验证
            # 密码长度验证
            # 更多的其它验证.....
            try:
                user = users.objects.get(email=email)
                request.session["username"]=user.username
                print( request.session["username"])
                if user.password == password:
                    request.session["email"]=email
                    request.session["send_ci"] = 0
                    return render(request, 'home.html', context={"username":request.session["username"]})
                else:
                    message = "密码不正确！"
            except:
                message = "邮箱不存在！"
        return render(request, 'login.html', {"message": message})
    return render(request, 'login.html')
def home(request):

    return render(request, 'home.html',context={"username":request.session["username"]})

def add_user(request):#注册
    if request.method == "POST":
        username=request.POST.get("username").strip()
        email = request.POST.get('email').strip()
        password = request.POST.get('password').strip()
        passwords = request.POST.get('passwords').strip()
        if not password==passwords:
            return render(request, 'add_user.html', {"message": "两次输入密码不同！"})
        #print(username,email,password)
        see = list(users.objects.values('email'))
        for i in see:
            if email == i['email']:
                return render(request, 'add_user.html', {"message": "邮箱已注册！"})
        request.session["username"] = username
        request.session["password"] = password
        request.session["email"] = email
        request.session["yzm"]=send_email(request.session["email"])
        return render(request, 'yanzhengma.html', {"message": "已向邮箱发送完毕 请输入验证码！"})
    return render(request, 'add_user.html')
def yanzhengma(request):
    val=request.session.get('email', default=None)
    if val == None:
        return render(request, 'login.html', {"message": "请登录！"})
    if request.method == "POST":
        yzm = request.POST.get('yzm').strip().lower()
        print("匹配验证码")
        if request.session["yzm"]==yzm:
            print("匹配正确清除")
            request.session["yzm"]=''
            user = users(username=request.session["username"], password=request.session["password"], email=request.session["email"])
            users.save(user)
            request.session["password"]=''
            return render(request, 'login.html', {"message": "注册成功！"})
        else:
            return render(request, 'yanzhengma.html', {"message": "验证码错误！"})

    return render(request, 'yanzhengma.html')
def no_open(request):#未开放
    return render(request, 'no_open.html')

def answer(request):#在线答题平台
    val=request.session.get('username', default=None)
    if val == None:
        return render(request, 'login.html', {"message": "请登录！"})
    print("跳转答题网页")
    return render(request, 'onlineExam/index.html')

def logout_user(request):#退出
    request.session.flush()
    print("清空退出")
    return redirect("/login/")

